package com.niannian.adapter;

import java.util.ArrayList;
import com.niannian.MyApplication;
import com.niannian.R;
import com.niannian.activity.DateTipActivity;
import com.niannian.activity.FamilyMemberDetailActivity;
import com.niannian.activity.RegardsActivity;
import com.niannian.activity.SystemNewsActivity;
import com.niannian.bean.MyFamilyMemberBean;
import com.niannian.bean.NewssBean;
import com.niannian.db.MyDBUser;
import com.niannian.util.Common;
import com.niannian.util.UserInfoManager;
import com.niannian.util.VAR;
import com.niannian.view.CircleImageView;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NewssAdaper extends BaseAdapter {
	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	private ArrayList<NewssBean> ndatas;
	private LayoutInflater inflater;
	private Activity mActivity;
	BaseAdapter adapter;

	public NewssAdaper(Activity activity) {
		super();
		this.mActivity = activity;
		inflater = LayoutInflater.from(mActivity);
		this.ndatas = new ArrayList<NewssBean>();
	}

	@Override
	public int getCount() {
		return ndatas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return ndatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public ArrayList<NewssBean> getNDatasList() {
		return ndatas;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	@Override
	public View getView(final int position, View v, ViewGroup arg2) {
		ViewHolder holder = null;
		if (v == null) {
			v = inflater.inflate(R.layout.item_newss, null);
			holder = new ViewHolder();
			holder.ll_item_head = (LinearLayout) v.findViewById(R.id.ll_item_head);
			holder.riv_avatar = (CircleImageView) v.findViewById(R.id.riv_avatar);
			holder.riv_avatar_p = (CircleImageView) v.findViewById(R.id.riv_avatar_p);
			holder.tv_name = (TextView) v.findViewById(R.id.tv_name);
			holder.tv_news_info = (TextView) v.findViewById(R.id.tv_news_info);

			holder.tv_news_count = (TextView) v.findViewById(R.id.tv_news_count);
			holder.tv_news_time = (TextView) v.findViewById(R.id.tv_news_time);

			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		final NewssBean Ndata = ndatas.get(position);

		if ("S".equals(Ndata.getMsg_type())) {
			holder.riv_avatar.setImageResource(R.drawable.system_message);
			holder.tv_name.setText("系统消息");
		} else {
			holder.tv_name.setText(Ndata.getSender_nickname());
			MyApplication.finalbitmap.display(holder.riv_avatar, Common.getAvatarThumbs(Ndata.getSender_avatar()));
		}

		holder.tv_news_info.setText(Ndata.getItem_content());
		holder.tv_news_time.setText(Common.sgmdate(Ndata.getItem_time()));

		if (Ndata.getMsg_count() > 0) {
			int msgcount = Ndata.getMsg_count();
			if (msgcount > 99) {
				msgcount = 99;
			}
			holder.tv_news_count.setVisibility(View.VISIBLE);
			holder.tv_news_count.setText(msgcount + "");
		} else {
			holder.tv_news_count.setVisibility(View.GONE);
		}

		holder.ll_item_head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (System.currentTimeMillis() - VAR.ExitTime > VAR.INTERVAL) {

					if ("R".equals(Ndata.getMsg_type())) {
						Intent intent = new Intent(mActivity, RegardsActivity.class);
						intent.putExtra("id", Ndata.getSender());
						mActivity.startActivity(intent);
						mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					} else if ("T".equals(Ndata.getMsg_type())) {
						Intent intent = new Intent(mActivity, DateTipActivity.class);
						intent.putExtra("id", Ndata.getSender());
						mActivity.startActivity(intent);
						mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					} else if ("S".equals(Ndata.getMsg_type())) {
						Intent intent = new Intent(mActivity, SystemNewsActivity.class);
						intent.putExtra("id", Ndata.getSender());
						mActivity.startActivity(intent);
						mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					}
					NotificationManager notiManager = (NotificationManager) mActivity.getSystemService(mActivity.NOTIFICATION_SERVICE);

					notiManager.cancelAll();
					VAR.ExitTime = System.currentTimeMillis();
				}
			}
		});

		if ("R".equals(Ndata.getMsg_type())) {
			holder.riv_avatar_p.setVisibility(View.VISIBLE);
			holder.tv_name.setVisibility(View.VISIBLE);
			holder.riv_avatar_p.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					userOnc(Ndata, position);
				}
			});
//			holder.tv_name.setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					userOnc(Ndata, position);
//				}
//			});
		} else {
			holder.riv_avatar_p.setVisibility(View.GONE);
			holder.tv_name.setVisibility(View.GONE);
		}

		return v;
	}

	private void userOnc(NewssBean fdata, int position) {

		MyFamilyMemberBean fd = new MyFamilyMemberBean();
		fd.setId(fdata.getSender());
		// fd.setFamily(fdata.getFamily());
		fd.setNickname(fdata.getSender_nickname());
		fd.setAvatar(fdata.getSender_avatar());

		Intent intent = new Intent(mActivity, FamilyMemberDetailActivity.class);
		intent.putExtra("position", position);
		intent.putExtra("data", fd);
		mActivity.startActivity(intent);
		mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

	}

	class ViewHolder {
		LinearLayout ll_item_head;
		CircleImageView riv_avatar, riv_avatar_p;
		TextView tv_name;
		TextView tv_news_info;
		TextView tv_news_count;
		TextView tv_news_time;

	}

}
